package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: wenjingyuan
 * @Date: 2023/10/09/10:17
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "站内信")
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;
    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接收方Id") @RequestParam("receiveUserId") @NonNull Long  receiveUserId,
                          @ApiParam("内容") @RequestParam("content") @NonNull String content){
        //1.如果我们的id没有参数，那么就从session里获取用户信息
        //false代表如果没有不创建session
        //检验当前登录用户的状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user==null || user.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //用户已禁言
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发
        if(receiveUserId==user.getId()){
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        //检验接收方是否存在
        User receiveUser=userService.selectById(receiveUserId);
        if(receiveUser==null|| receiveUser.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //构建对象
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setContent(content);
        message.setReceiveUserId(receiveUserId);
        //调用service
        messageService.create(message);
        return AppResult.success("发送成功");
    }
    @ApiOperation("站内信未读数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        Integer integer = messageService.selectUnreadCount(user.getId());
        return AppResult.success(integer);
    }

    @GetMapping("/getAll")
    @ApiOperation("根据接受者id查询站内信列表")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //调用service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }

    /**
     * 更新站内信的状态
     * @param id
     * @return
     */
    @ApiOperation("更新站内信的状态更新为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
            @ApiParam("站内信id") @RequestParam("id") @NonNull Long id){
        //根据id查询站内信
        Message message = messageService.selectById(id);
        //站内信不存在
        if(message==null || message.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //站内信是不是自己的
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()!=message.getReceiveUserId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        //调用service
        messageService.updateState(id,(byte)1);
        return AppResult.success();
    }

    /**
     * 回复站内信
     * @param request
     * @param repliedId
     * @param content
     * @return
     */
    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public  AppResult reply(HttpServletRequest request,
                            @ApiParam("要回复的站内信id") @RequestParam("repliedId") @NonNull Long repliedId,
                            @ApiParam("内容") @RequestParam("content") @NonNull String content){
        //检验当前登录用户的状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //用户已禁言
        if(user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //检验要回复的站内信状态
        Message updateMessage = messageService.selectById(repliedId);
        if(updateMessage==null){
            //返回错误信息
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能给自己回复
        if(user.getId()==updateMessage.getPostUserId()){
            return AppResult.failed(ResultCode.FAILED);
        }
        //构造对象
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(updateMessage.getPostUserId());
        message.setContent(content);
        //调用service
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}
